<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Mongo | 兜里有糖</title><meta name="keywords" content="blog,编程,mongo"><meta name="author" content="兜里有糖"><meta name="copyright" content="兜里有糖"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="安装1. 方法一(不推荐)1vi &#x2F;etc&#x2F;yum.repos.d&#x2F;mongodb.repo 写入 12345[mongodb-org]name&#x3D;MongoDB Repositorybaseurl&#x3D;https:&#x2F;&#x2F;mirrors.tuna.tsinghua.edu.cn&#x2F;mongodb&#x2F;yum&#x2F;el">
<meta property="og:type" content="article">
<meta property="og:title" content="Mongo">
<meta property="og:url" content="https://blog.cwxhmk.top/2020/08/24/program/2020-08-24-mongo/index.html">
<meta property="og:site_name" content="兜里有糖">
<meta property="og:description" content="安装1. 方法一(不推荐)1vi &#x2F;etc&#x2F;yum.repos.d&#x2F;mongodb.repo 写入 12345[mongodb-org]name&#x3D;MongoDB Repositorybaseurl&#x3D;https:&#x2F;&#x2F;mirrors.tuna.tsinghua.edu.cn&#x2F;mongodb&#x2F;yum&#x2F;el">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://image.cwxhmk.top/blog/default_cover.jpg">
<meta property="article:published_time" content="2020-08-24T22:07:42.000Z">
<meta property="article:modified_time" content="2022-01-04T18:31:04.998Z">
<meta property="article:author" content="兜里有糖">
<meta property="article:tag" content="blog">
<meta property="article:tag" content="编程">
<meta property="article:tag" content="mongo">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://image.cwxhmk.top/blog/default_cover.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://blog.cwxhmk.top/2020/08/24/program/2020-08-24-mongo/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?a14889b83a055b58cc3b9baac0e8d397";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Mongo',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-01-04 18:31:04'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><meta name="baidu-site-verification" content="code-DLRo0M1JYB" /><meta name="msvalidate.01" content="68CBEB31DECE622B51F762A579742691" /><meta name="google-site-verification" content="EbiglcFzq1VMWmp5uIo4rPO8dnEVWbr06jCFe_BGHfo" /><link rel="stylesheet" href="/css/person.css"><meta name="generator" content="Hexo 5.1.1"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src= "/img/loading.gif" data-lazy-src="https://image.cwxhmk.top/blog/tou.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">14</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">11</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">1</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间线</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 列表</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://image.cwxhmk.top/blog/default_cover.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">兜里有糖</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间线</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 列表</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Mongo</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-08-24T22:07:42.000Z" title="发表于 2020-08-24 22:07:42">2020-08-24</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-01-04T18:31:04.998Z" title="更新于 2022-01-04 18:31:04">2022-01-04</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%BC%96%E7%A8%8B/">编程</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.4k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>6分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Mongo"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span><span class="post-meta-separator">|</span><span class="post-meta-commentcount"><i class="far fa-comments fa-fw post-meta-icon"></i><span class="post-meta-label">评论数:</span><a href="/2020/08/24/program/2020-08-24-mongo/#post-comment"><span class="waline-comment-count" id="/2020/08/24/program/2020-08-24-mongo/"></span></a></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h1><h2 id="1-方法一-不推荐"><a href="#1-方法一-不推荐" class="headerlink" title="1. 方法一(不推荐)"></a>1. 方法一(不推荐)</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vi &#x2F;etc&#x2F;yum.repos.d&#x2F;mongodb.repo</span><br></pre></td></tr></table></figure>
<p>写入</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[mongodb-org]</span><br><span class="line">name&#x3D;MongoDB Repository</span><br><span class="line">baseurl&#x3D;https:&#x2F;&#x2F;mirrors.tuna.tsinghua.edu.cn&#x2F;mongodb&#x2F;yum&#x2F;el$releasever&#x2F;</span><br><span class="line">gpgcheck&#x3D;0</span><br><span class="line">enabled&#x3D;1</span><br></pre></td></tr></table></figure>
<p>保存</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo yum makecache</span><br><span class="line">sudo yum install mongodb-org</span><br></pre></td></tr></table></figure>
<p>将mongoDB添加到systemd</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim &#x2F;usr&#x2F;lib&#x2F;systemd&#x2F;system&#x2F;mongod.service</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">[Unit]</span><br><span class="line">Description&#x3D;mongodb database</span><br><span class="line"></span><br><span class="line">[Service]</span><br><span class="line">User&#x3D;mongod</span><br><span class="line">Group&#x3D;mongod</span><br><span class="line">Environment&#x3D;&quot;OPTIONS&#x3D;--quiet -f &#x2F;etc&#x2F;mongod.conf&quot;</span><br><span class="line">ExecStart&#x3D;&#x2F;usr&#x2F;bin&#x2F;mongod $OPTIONS run</span><br><span class="line">PIDFile&#x3D;&#x2F;var&#x2F;run&#x2F;mongodb&#x2F;mongod.pid</span><br><span class="line"></span><br><span class="line">[Install]</span><br><span class="line">WantedBy&#x3D;multi-user.target</span><br></pre></td></tr></table></figure>
<p>建立链接</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ln -s &#x2F;usr&#x2F;lib&#x2F;systemd&#x2F;system&#x2F;mongod.service &#x2F;etc&#x2F;systemd&#x2F;system&#x2F;multi-user.target.wants&#x2F;</span><br></pre></td></tr></table></figure>
<p>重新加载systemctl</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl daemon-reload</span><br></pre></td></tr></table></figure>

<h2 id="2-方法二-推荐"><a href="#2-方法二-推荐" class="headerlink" title="2. 方法二(推荐)"></a>2. 方法二(推荐)</h2><p>###1. 安装</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># 下载</span><br><span class="line">curl -O https:&#x2F;&#x2F;fastdl.mongodb.org&#x2F;linux&#x2F;mongodb-linux-x86_64-3.0.6.tgz    </span><br><span class="line"># 解压</span><br><span class="line">tar -zxvf mongodb-linux-x86_64-3.0.6.tgz                                   </span><br><span class="line"># 将解压包拷贝到指定目录</span><br><span class="line">mv  mongodb-linux-x86_64-3.0.6&#x2F; &#x2F;usr&#x2F;local&#x2F;mongodb                         </span><br></pre></td></tr></table></figure>
<p>MongoDB 的可执行文件位于 bin 目录下，所以可以将其添加到 PATH 路径中：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">export PATH&#x3D;&#x2F;usr&#x2F;local&#x2F;mongodb&#x2F;bin:$PATH</span><br></pre></td></tr></table></figure>
<p>直接执行此命令，只会创建出临时的环境变量，即重新断开连接服务器后会失效;</p>
<p>环境变量持久化配置：需要将mongod路径添加到系统路径中，<br>在 <code>/etc/profile</code> 文件中，添加</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">export PATH&#x3D;&#x2F;usr&#x2F;local&#x2F;mongodb&#x2F;bin:$PATH;</span><br></pre></td></tr></table></figure>
<p>执行 <code>source /etc/profile</code>使系统环境变量立即生效</p>
<h3 id="2-启动配置"><a href="#2-启动配置" class="headerlink" title="2.启动配置"></a>2.启动配置</h3><ol>
<li>创建数据库存放和日志目录<br> <code>/usr/local/mongodb/data/db</code><br> <code>/usr/local/mongodb/log</code></li>
<li>配置mongod启动文件<br><code>/usr/local/mongodb/etc</code>下创建配置文件mongod.conf:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">dbpath&#x3D;&#x2F;usr&#x2F;local&#x2F;mongodb&#x2F;data   # 数据库存放位置（之前创建的）</span><br><span class="line">logpath&#x3D;&#x2F;usr&#x2F;local&#x2F;mongodb&#x2F;log&#x2F;mongo.log   # 数据库日志存放位置（之前创建的）</span><br><span class="line">port&#x3D;27017</span><br><span class="line">fork&#x3D;true   #后台运行</span><br><span class="line">auth&#x3D;false  # 初次配置先关了权限验证登陆模式</span><br><span class="line">journal&#x3D;false</span><br></pre></td></tr></table></figure>
<h3 id="3-启动"><a href="#3-启动" class="headerlink" title="3.启动"></a>3.启动</h3></li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">cd &#x2F;usr&#x2F;local&#x2F;mongodb&#x2F;bin</span><br><span class="line">mongod -f &#x2F;usr&#x2F;local&#x2F;mongodb&#x2F;etc&#x2F;mongo.conf</span><br><span class="line">#关闭</span><br><span class="line">mongod -f &#x2F;usr&#x2F;local&#x2F;mongodb&#x2F;etc&#x2F;mongo.conf -shutdown</span><br></pre></td></tr></table></figure>

<h3 id="4-设置用户"><a href="#4-设置用户" class="headerlink" title="4.设置用户"></a>4.设置用户</h3><p>进入数据库管理命令界面:<code>mongo</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">use admin</span><br><span class="line">db.createUser(&#123;user:&#39;admin&#39;,pwd:&#39;password&#39;,roles:[&#123;role:&#39;root&#39;,&#39;db&#39;:&#39;admin&#39;&#125;]&#125;)</span><br><span class="line">use dbname</span><br><span class="line">db.createUser(&#123;user:&#39;dbname&#39;,pwd:&#39;password&#39;,roles:[&#123;role:&#39;readWrite&#39;,db:&#39;dbname&#39;&#125;]&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<ul>
<li>常用的role值记录: </li>
</ul>
<ol>
<li>数据库用户角色：read、readWrite; </li>
<li>数据库管理角色：dbAdmin、dbOwner、userAdmin； </li>
<li>集群管理角色：clusterAdmin、clusterManager、clusterMonitor、hostManager； </li>
<li>备份恢复角色：backup、restore； </li>
<li>所有数据库角色：readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase </li>
<li>超级用户角色：root </li>
<li>内部角色：__system </li>
</ol>
<ul>
<li>相应的功能 </li>
<li>Read：允许用户读取指定数据库 </li>
<li>readWrite：允许用户读写指定数据库 </li>
<li>dbAdmin：允许用户在指定数据库中执行管理函数，如索引创建、删除，查看统计或访问system.profile </li>
<li>userAdmin：允许用户向system.users集合写入，可以找指定数据库里创建、删除和管理用户 </li>
<li>clusterAdmin：只在admin数据库中可用，赋予用户所有分片和复制集相关函数的管理权限。 </li>
<li>readAnyDatabase：只在admin数据库中可用，赋予用户所有数据库的读权限 </li>
<li>readWriteAnyDatabase：只在admin数据库中可用，赋予用户所有数据库的读写权限 </li>
<li>userAdminAnyDatabase：只在admin数据库中可用，赋予用户所有数据库的userAdmin权限 </li>
<li>dbAdminAnyDatabase：只在admin数据库中可用，赋予用户所有数据库的dbAdmin权限。 </li>
<li>root：只在admin数据库中可用。超级账号，超级权限</li>
</ul>
<p>还把配置文件<code>auth</code>改为true</p>
<h3 id="6-远程连接"><a href="#6-远程连接" class="headerlink" title="6.远程连接"></a>6.远程连接</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">mongo IP:27017&#x2F;admin -u useradmin -p password</span><br><span class="line">或</span><br><span class="line">mongo IP:27017</span><br><span class="line">use dbname</span><br><span class="line">db.auth(&#39;username&#39;,&#39;password&#39;)即可</span><br></pre></td></tr></table></figure>


<h3 id="备份与恢复"><a href="#备份与恢复" class="headerlink" title="备份与恢复"></a>备份与恢复</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">* 备份</span><br><span class="line">    mongodump -h dbhost -d dbname -o dbdirectory</span><br><span class="line">    mongodump -h ip:27017 -u admin -p password -d database -o path</span><br><span class="line">    -h：</span><br><span class="line">MongDB所在服务器地址，例如：127.0.0.1，当然也可以指定端口号：127.0.0.1:27017</span><br><span class="line"></span><br><span class="line">    -d：</span><br><span class="line">需要备份的数据库实例，例如：test</span><br><span class="line"></span><br><span class="line">    -o：</span><br><span class="line">备份的数据存放位置，例如：c:\data\dump，当然该目录需要提前建立，在备份完成后，系统自动在dump目录下建立一个test目录，这个目录里面存放该数据库实例的备份数据。</span><br><span class="line">* 恢复</span><br><span class="line"></span><br><span class="line">    mongorestore -h ip:port -d dbname 路径</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    mongorestore -h &lt;hostname&gt;&lt;:port&gt; -d dbname &lt;path&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    --host &lt;:port&gt;, -h &lt;:port&gt;：</span><br><span class="line">    MongoDB所在服务器地址，默认为： localhost:27017</span><br><span class="line"></span><br><span class="line">    --db , -d ：</span><br><span class="line">    需要恢复的数据库实例，例如：test，当然这个名称也可以和备份时候的不一样，比如test2</span><br><span class="line"></span><br><span class="line">    --drop：</span><br><span class="line">    恢复的时候，先删除当前数据，然后恢复备份的数据。就是说，恢复后，备份后添加修改的数据都会被删除，慎用哦！</span><br><span class="line"></span><br><span class="line">    &lt;path&gt;：</span><br><span class="line">    mongorestore 最后的一个参数，设置备份数据所在位置，例如：c:\data\dump\test。</span><br><span class="line"></span><br><span class="line">    你不能同时指定 &lt;path&gt; 和 --dir 选项，--dir也可以设置备份目录。</span><br><span class="line"></span><br><span class="line">    --dir：</span><br><span class="line">    指定备份的目录</span><br><span class="line"></span><br><span class="line">    你不能同时指定 &lt;path&gt; 和 --dir 选项。</span><br></pre></td></tr></table></figure>
<h3 id="5-卸载"><a href="#5-卸载" class="headerlink" title="5. 卸载"></a>5. 卸载</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">sudo yum erase $(rpm -qa | grep mongodb-org)   # 卸载</span><br><span class="line"></span><br><span class="line">sudo rm -r &#x2F;var&#x2F;log&#x2F;mongodb  # 删除日志文件</span><br><span class="line"></span><br><span class="line">sudo rm -r &#x2F;var&#x2F;lib&#x2F;mongo    # 删除数据</span><br></pre></td></tr></table></figure>
<h3 id="6-操作"><a href="#6-操作" class="headerlink" title="6.操作"></a>6.操作</h3><p>查重</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">db.getCollection(&#39;表&#39;).aggregate([</span><br><span class="line">    &#123;</span><br><span class="line">        $group:&#123;_id:&#123;&#39;字段&#39;:&#39;$字段&#39;&#125;, count:&#123;$sum:1&#125;&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">        $match:&#123;count:&#123;$gt:1&#125;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">])</span><br></pre></td></tr></table></figure>


<p>按netid进行查重<br>去处</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">db.getCollection(&#39;表&#39;).aggregate([</span><br><span class="line">    &#123;</span><br><span class="line">        $group: &#123; _id: &#123;字段: &#39;$字段&#39;&#125;,count: &#123;$sum: 1&#125;,dups: &#123;$addToSet: &#39;$_id&#39;&#125;&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">        $match: &#123;count: &#123;$gt: 1&#125;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">]).forEach(function(doc)&#123;</span><br><span class="line">    doc.dups.shift();</span><br><span class="line">    db.表.remove(&#123;_id: &#123;$in: doc.dups&#125;&#125;);</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">去处指定字段</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">兜里有糖</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://blog.cwxhmk.top/2020/08/24/program/2020-08-24-mongo/">https://blog.cwxhmk.top/2020/08/24/program/2020-08-24-mongo/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://blog.cwxhmk.top" target="_blank">兜里有糖</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/blog/">blog</a><a class="post-meta__tags" href="/tags/%E7%BC%96%E7%A8%8B/">编程</a><a class="post-meta__tags" href="/tags/mongo/">mongo</a></div><div class="post_share"><div class="social-share" data-image="https://image.cwxhmk.top/blog/default_cover.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2020/08/24/program/2020-08-24-Linux常见命令/" title="Linux常见命令"><img class="cover" src= "/img/loading.gif" data-lazy-src="https://image.cwxhmk.top/blog/202008251.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-08-24</div><div class="title">Linux常见命令</div></div></a></div><div><a href="/2020/08/24/program/2020-08-24-Nginx安装与配置/" title="nginx安装与配置"><img class="cover" src= "/img/loading.gif" data-lazy-src="https://image.cwxhmk.top/blog/default_cover.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-08-24</div><div class="title">nginx安装与配置</div></div></a></div><div><a href="/2020/08/24/program/2020-08-24-Python-Install/" title="Python安装"><img class="cover" src= "/img/loading.gif" data-lazy-src="https://image.cwxhmk.top/blog/python.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-08-24</div><div class="title">Python安装</div></div></a></div><div><a href="/2020/08/24/program/2020-08-24-并发测试/" title="并发测试"><img class="cover" src= "/img/loading.gif" data-lazy-src="https://image.cwxhmk.top/blog/default_cover.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-08-24</div><div class="title">并发测试</div></div></a></div><div><a href="/2020/08/24/program/2020-08-24-部署/" title="部署"><img class="cover" src= "/img/loading.gif" data-lazy-src="https://image.cwxhmk.top/blog/gunicron.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-08-24</div><div class="title">部署</div></div></a></div><div><a href="/2020/09/18/program/2020-09-18-jupyter/" title="Jupyter搭建"><img class="cover" src= "/img/loading.gif" data-lazy-src="https://image.cwxhmk.top/blog/jupyter.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-09-18</div><div class="title">Jupyter搭建</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="waline-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src= "/img/loading.gif" data-lazy-src="https://image.cwxhmk.top/blog/tou.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">兜里有糖</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">14</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">11</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">1</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/dlytang"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/dlytang" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="/472548557" target="_blank" title="QQ"><i class="fab fa-qq"></i></a><a class="social-icon" href="mailto:472548557@qq.com" target="_blank" title="QQ"><i class="fab fa-envelope-o"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">欢迎</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AE%89%E8%A3%85"><span class="toc-number">1.</span> <span class="toc-text">安装</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E6%96%B9%E6%B3%95%E4%B8%80-%E4%B8%8D%E6%8E%A8%E8%8D%90"><span class="toc-number">1.1.</span> <span class="toc-text">1. 方法一(不推荐)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E6%96%B9%E6%B3%95%E4%BA%8C-%E6%8E%A8%E8%8D%90"><span class="toc-number">1.2.</span> <span class="toc-text">2. 方法二(推荐)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%90%AF%E5%8A%A8%E9%85%8D%E7%BD%AE"><span class="toc-number">1.2.1.</span> <span class="toc-text">2.启动配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E5%90%AF%E5%8A%A8"><span class="toc-number">1.2.2.</span> <span class="toc-text">3.启动</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E8%AE%BE%E7%BD%AE%E7%94%A8%E6%88%B7"><span class="toc-number">1.2.3.</span> <span class="toc-text">4.设置用户</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-%E8%BF%9C%E7%A8%8B%E8%BF%9E%E6%8E%A5"><span class="toc-number">1.2.4.</span> <span class="toc-text">6.远程连接</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%87%E4%BB%BD%E4%B8%8E%E6%81%A2%E5%A4%8D"><span class="toc-number">1.2.5.</span> <span class="toc-text">备份与恢复</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E5%8D%B8%E8%BD%BD"><span class="toc-number">1.2.6.</span> <span class="toc-text">5. 卸载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-%E6%93%8D%E4%BD%9C"><span class="toc-number">1.2.7.</span> <span class="toc-text">6.操作</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/08/22/program/2021-08-22-coding/" title="Coding"><img src= "/img/loading.gif" data-lazy-src="https://image.cwxhmk.top/blog/coding.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Coding"/></a><div class="content"><a class="title" href="/2021/08/22/program/2021-08-22-coding/" title="Coding">Coding</a><time datetime="2021-08-22T21:20:19.000Z" title="发表于 2021-08-22 21:20:19">2021-08-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/08/22/program/2021-08-22-linuxTowindows/" title="Linux Windows"><img src= "/img/loading.gif" data-lazy-src="https://image.cwxhmk.top/blog/linux_windows.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Linux Windows"/></a><div class="content"><a class="title" href="/2021/08/22/program/2021-08-22-linuxTowindows/" title="Linux Windows">Linux Windows</a><time datetime="2021-08-22T21:16:19.000Z" title="发表于 2021-08-22 21:16:19">2021-08-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/07/16/program/2021-07-16-wechaty/" title="Wechaty"><img src= "/img/loading.gif" data-lazy-src="https://image.cwxhmk.top/blog/wechaty.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Wechaty"/></a><div class="content"><a class="title" href="/2021/07/16/program/2021-07-16-wechaty/" title="Wechaty">Wechaty</a><time datetime="2021-07-16T21:16:19.000Z" title="发表于 2021-07-16 21:16:19">2021-07-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/05/15/program/2021-05-15-cloudreve/" title="个人网盘"><img src= "/img/loading.gif" data-lazy-src="https://image.cwxhmk.top/blog/cloudreve.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="个人网盘"/></a><div class="content"><a class="title" href="/2021/05/15/program/2021-05-15-cloudreve/" title="个人网盘">个人网盘</a><time datetime="2021-05-15T21:16:19.000Z" title="发表于 2021-05-15 21:16:19">2021-05-15</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/05/08/program/2021-05-08-docker/" title="docker"><img src= "/img/loading.gif" data-lazy-src="https://image.cwxhmk.top/blog/docker.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="docker"/></a><div class="content"><a class="title" href="/2021/05/08/program/2021-05-08-docker/" title="docker">docker</a><time datetime="2021-05-08T20:16:19.000Z" title="发表于 2021-05-08 20:16:19">2021-05-08</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://image.cwxhmk.top/blog/default_cover.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By 兜里有糖</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a target="_blank" rel="noopener" href="https://www.upyun.com/?utm_source=lianmeng&utm_medium=referral">本站由<img src= "/img/loading.gif" data-lazy-src="/img/logo5.png" class="upyun"/> 提供CDN加速</a> <div href="https://beian.miit.gov.cn/#/Integrated/index"><img class="icp-icon" src= "/img/loading.gif" data-lazy-src="/img/icp.png"><span>备案号：陕ICP备20000393号</span></div></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>function loadWaline () {
  function initWaline () {
    const waline = new Waline(Object.assign({
      el: '#waline-wrap',
      serverURL: 'https://blog-weld-phi.vercel.app/',
      avatar: 'monsterid',
      path: location.pathname,
      emojiCDN: '//i0.hdslb.com/bfs/emote/',
      emojiMaps: {"tv_doge":"6ea59c827c414b4a2955fe79e0f6fd3dcd515e24.png","tv_亲亲":"a8111ad55953ef5e3be3327ef94eb4a39d535d06.png","tv_偷笑":"bb690d4107620f1c15cff29509db529a73aee261.png","tv_再见":"180129b8ea851044ce71caf55cc8ce44bd4a4fc8.png","tv_冷漠":"b9cbc755c2b3ee43be07ca13de84e5b699a3f101.png","tv_发怒":"34ba3cd204d5b05fec70ce08fa9fa0dd612409ff.png","tv_发财":"34db290afd2963723c6eb3c4560667db7253a21a.png","tv_可爱":"9e55fd9b500ac4b96613539f1ce2f9499e314ed9.png","tv_吐血":"09dd16a7aa59b77baa1155d47484409624470c77.png","tv_呆":"fe1179ebaa191569b0d31cecafe7a2cd1c951c9d.png","tv_呕吐":"9f996894a39e282ccf5e66856af49483f81870f3.png","tv_困":"241ee304e44c0af029adceb294399391e4737ef2.png","tv_坏笑":"1f0b87f731a671079842116e0991c91c2c88645a.png","tv_大佬":"093c1e2c490161aca397afc45573c877cdead616.png","tv_大哭":"23269aeb35f99daee28dda129676f6e9ea87934f.png","tv_委屈":"d04dba7b5465779e9755d2ab6f0a897b9b33bb77.png","tv_害羞":"a37683fb5642fa3ddfc7f4e5525fd13e42a2bdb1.png","tv_尴尬":"7cfa62dafc59798a3d3fb262d421eeeff166cfa4.png","tv_微笑":"70dc5c7b56f93eb61bddba11e28fb1d18fddcd4c.png","tv_思考":"90cf159733e558137ed20aa04d09964436f618a1.png","tv_惊吓":"0d15c7e2ee58e935adc6a7193ee042388adc22af.png"},
      visitor: false,
      dark: '[data-theme="dark"]'
    }, null))
  }

  if (typeof Waline === 'function') initWaline() 
  else getScript('https://cdn.jsdelivr.net/npm/@waline/client/dist/Waline.min.js').then(initWaline)
}

if ('Waline' === 'Waline' || !false) {
  if (false) btf.loadComment(document.getElementById('waline-wrap'),loadWaline)
  else setTimeout(loadWaline, 0)
} else {
  function loadOtherComment () {
    loadWaline()
  }
}</script></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>